package web;

import api.CollectionManager;
import api.DatabaseManager;
import htmlparser.HtmlParser;
import htmlparser.HtmlParserException;
import htmlparser.HttpConnectionReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.basex.core.BaseXException;

/**
 *
 * @author Football-Statistics Team
 */
public class ParseMatchServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Locale locale = request.getLocale();
        ResourceBundle labelsBundle;
        labelsBundle = ResourceBundle.getBundle("labelsBundle", locale);

        String lid = request.getParameter("lid");
        String mid = request.getParameter("mid");
        request.setAttribute("lid", lid);
        
        DatabaseManager dm = new DatabaseManager();
        CollectionManager cm = new CollectionManager(lid, dm);
        
        HttpConnectionReader reader = null;
        try {
            request.setAttribute("xml_leagues", cm.getLeagueList());
            
            reader = new HttpConnectionReader(cm.getParserURL());
            reader.connect();
            InputStream in = reader.getHttpInputStream();
            HtmlParser parser = new HtmlParser( in );
            Map<String, String> links = parser.extractLinks("http://www.eurofotbal.cz");
            in.close();
            String newMatches = cm.getNewMatches(links);
            if (!newMatches.equals("")) {
                request.setAttribute("xml_matches", cm.getNewMatches(links));
            }
            if (mid != null && !mid.equals("")) {              
                reader = new HttpConnectionReader(links.get(mid));
                reader.connect();
                parser.setDocument( reader.getHttpInputStream() );
                request.setAttribute("mid", mid);
                request.setAttribute("xml_match", parser.extractMatchInfo());
                request.getRequestDispatcher("/addMatch.jsp").forward(request, response);
                return;
            }            
            request.getRequestDispatcher("/parseMatch.jsp").forward(request, response);
            return;
        } catch (HtmlParserException ex) {
            Logger.getLogger(ParseMatchServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BaseXException ex) {
            Logger.getLogger(ParseMatchServlet.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", labelsBundle.getString("database_error"));
            request.getRequestDispatcher("/parseMatch.jsp").forward(request, response);
            return;
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
